package com.zj.leetcode.medium.hash;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @program: algorithm
 * @description: 数组中的 k-diff 数对
 * 532 k-diff-pairs-in-an-array
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class KDiffPairsInAnArray {
    public static void main(String[] args) {
//        int[] nums = new int[]{3, 1, 4, 1, 5};
//        int k = 2;
//        int[] nums = {1, 2, 3, 4, 5};
//        int k = 1;
//        int[] nums = {1, 3, 1, 4, 4};
//        int k = 2;
//        [3,1,4,1,5]
//        2
        int[] nums = {3,1,4,1,5};
        int k = 2;
        Solution solution = new KDiffPairsInAnArray().new Solution();
        System.out.println(solution.findPairs(nums, k));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int findPairs(int[] nums, int k) {

            HashMap<Integer, Integer> map = new HashMap<>();
            int count = 0;

            int l = nums.length;

            if (l <= 1) {
                return 0;
            }
            Arrays.sort(nums);
            for (int i = 0; i < l; i++) {
                for (int j = i + 1; j < l; j++) {
                    if (Math.abs(nums[i] - nums[j]) == k) {
                        if(!map.containsKey(nums[i]) && !map.containsKey(nums[j])){
                            count++;
                        }
                        map.put(nums[i], nums[j]);
                    }
                }
            }

            return count;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
